// import { useUserInfo } from '@/store/user';
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';

// export { pandaAxios };
export class Paxios {
  public static instance = null;
  private axios: AxiosInstance | null = null;
  private constructor() {
    this.axios = axios.create({
      withCredentials: true,
      responseType: 'json',
      timeout: 1000 * 60,
      baseURL: import.meta.env.VITE_API_BASE,
      /* tslint:disable */
      // baseURL: import.meta.env.VITE_BASE_URL,
      // baseURL: 'http://10.168.1.130:7077',
      // baseURL: 'http://10.168.1.130:7077',
      // baseURL: process.env.VUE_APP_ENV && process.env.VUE_APP_ENV === 'dev' ? '' : 'https://pms.lingzhuyunpms.cn/',
    });
    this.initInterceptors();
  }
  static getInstance(): Paxios {
    if (this.instance) {
      return this.instance;
    } else {
      return new Paxios();
    }
  }

  private initInterceptors() {
    if (!this.axios) return;
    /**
     * 拦截Request
     */
    this.axios.interceptors.request.use(
      (res: InternalAxiosRequestConfig) => {
        if (res.params && res.params.localhost) {
          res.baseURL = res.params.localhost;
        }
        // res.headers!['AUTHTOKEN'] = useUserInfo().getToken || '';
        return res;
      },
      (error: any) => Promise.reject(error)
    );
    // this.axios.interceptors.request.use((req => {
    //   this.axios.
    // }))
    this.axios.interceptors.response.use(
      (response: AxiosResponse) => {
        if (response.status >= 200 && response.status < 400) {
          const data = response.data;
          if (data) {
            if (data.code == 200) {
              return data;
            } else if (data.code === 403 || data.code === 886) {
              // console.log(response.request.responseURL.indexOf('/admin/loginOut'), response.request.responseURL);
              // if (response.request.responseURL.indexOf('/admin/loginOut') === -1) {
              //     this.clearLoginInfo();
              // } else {
              //     routers.push({
              //         name: "login"
              //     });
              // }
              // useUserInfo().token = '';
              return Promise.resolve(data);
            } else {
              return Promise.reject(data);
            }
          }
        }
        return response;
      },
      (error: any) => {
        // 网络无响应,404,50X
        return Promise.reject(error);
      }
    );
  }
  /**
   * 清理登录信息
   */
  // private clearLoginInfo() {
  //     try {
  //         if (useUserInfo().getToken) {
  //             useUserInfo().logOut();
  //             setTimeout(() => {
  //                 routers.push({
  //                     path: RouterConfig.Login.fullPath,
  //                 });
  //             }, 100);
  //         }
  //     } catch (error) {
  //         routers.push({
  //             path: RouterConfig.Login.fullPath,
  //         });
  //     }
  // }

  public get<T, D>(url: string, params?: D): Promise<T> {
    return this.axios!.get<any, T, any>(url, { params });
  }

  public post<T, D>(url: string, data?: D, config?: AxiosRequestConfig): Promise<T> {
    return this.axios!.post(url, data, config);
  }
}
